package com.example.noobpackage.ui.selfpackage

import android.content.Context
import android.os.Bundle
import android.transition.TransitionInflater
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.noobpackage.R
import com.example.noobpackage.tool.MyApplication
import com.example.noobpackage.ui.MainViewModel
import kotlinx.android.synthetic.main.fragment_self_package.*

class SelfPackageFragment() : Fragment() {

    private val shareViewModel: MainViewModel by activityViewModels()
    private val viewModel: SelfPackageViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        sharedElementEnterTransition =
            TransitionInflater.from(context).inflateTransition(android.R.transition.move)
                .setDuration(400)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_self_package, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        // 关闭输入法
        (context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(
            packagesListView.windowToken,
            InputMethodManager.HIDE_NOT_ALWAYS
        )

        // 建立View和数据的关联
        val layoutManager = LinearLayoutManager(MyApplication.context)
        packagesListView.layoutManager = layoutManager
        val listAdapter = SelfPackageAdapter()

        // 配置列表项点击事件
        listAdapter.setItemClick { position, extras ->
            if (!shareViewModel.getExpress(position)?.isAccepted!! && !shareViewModel.getExpress(
                    position
                )?.isPublished!!
            ) {
                // 获取对应的package并进行跳转
                val action = SelfPackageFragmentDirections.publishPackage(position)
                findNavController().navigate(action, extras)
            } else {
                val action =
                    SelfPackageFragmentDirections.actionMyPackageFragmentToOrderDetailFragment(
                        position
                    )
                findNavController().navigate(action, extras)
            }
        }

        // 配置列表项滑动删除事件
        listAdapter.onItemDismissCallback = {
            viewModel.completeLocalExpress(shareViewModel.getExpress(it)!!)
        }

        // 配置让列表加载完之后再触发过渡动画
        packagesListView.apply {
            this.adapter = listAdapter
            postponeEnterTransition()
            viewTreeObserver.addOnPreDrawListener {
                startPostponedEnterTransition()
                true
            }
        }

        // 配置滑动手势处理效果函数
        val callback = SelfPackageItemTouchHelperCallback(listAdapter)
        val itemTouchHelper = ItemTouchHelper(callback)
        itemTouchHelper.attachToRecyclerView(packagesListView)

        // 当数据更新时修改list
        shareViewModel.allExpressList.observe(viewLifecycleOwner, {
            listAdapter.setExpressData(it)
        })

        add.setOnClickListener {
            findNavController().navigate(R.id.add_package)
        }
    }
}